<?php
/*
	Obelix BB - Obelix Bulletin Board.
	
	Auteur : Fy - http://fee.lya.eu

	## Fonctions globales ##
*/


// Vérifier l'identification.
function check_auth() 
{
	if ($_SESSION['idu']) {
		return true;
	} else {
		return false;
	}
}

// Calcule l'âge en fonction de la date actuelle.
function age_calc($anniv1) 
{
	$anniv2 = explode('-',date('Y-m-d'));
	$age = $anniv2[0]-$anniv1[0];
	if(($anniv2[1]-$anniv1[1]) < 0) {
		$age--;
	} else if(($anniv2[2]-$anniv1[2]) <0) {
		$age--;
	}
	return $age;
}

// Users en ligne.
function update_online() 
{
	global $config;
	if(check_auth()) {
		$mysql = mysql_query('SELECT COUNT(id) FROM '.$config['mysql_prefix'].'users_online WHERE id="'.$_SESSION['idu'].'"');
		$result = mysql_result($mysql,0);
		if(!$result) {
			mysql_query('INSERT INTO  '.$config['mysql_prefix'].'users_online VALUES("'.$_SESSION['idu'].'","'.time().'")');
		} else {
			mysql_query('UPDATE  '.$config['mysql_prefix'].'users_online SET time="'.time().'"  WHERE id="'.$_SESSION['idu'].'"')or die(mysql_error());
		}
	} else {
		$mysql = mysql_query('SELECT COUNT(ip) FROM '.$config['mysql_prefix'].'guests_online WHERE ip="'.$_SERVER['REMOTE_ADDR'].'"');
		$result = mysql_result($mysql,0);
		if(!$result) {
			mysql_query('INSERT INTO  '.$config['mysql_prefix'].'guests_online VALUES("'.$_SERVER['REMOTE_ADDR'].'","'.time().'")');
		} else {
			mysql_query('UPDATE  '.$config['mysql_prefix'].'guests_online SET time="'.time().'" WHERE ip="'.$_SERVER['REMOTE_ADDR'].'"');
		}
	}
	mysql_query('DELETE FROM '.$config['mysql_prefix'].'users_online WHERE time<'.(time()-120).'');
	mysql_query('DELETE FROM '.$config['mysql_prefix'].'guests_online WHERE time<'.(time()-120).'');
}

// Système de pagination.
function per_page($nb, $page) {
	global $config;

	if(!$page) $page =1;

	$nb_page =ceil($nb/$config['board_per_page']);

	$borne_haut = $config['board_per_page'];
	$borne_bas = ($page*$config['board_per_page'])-($config['board_per_page']);

	$i = $page;
	while($i > 1 && $i > ($page-5)) {
		$i--;
		$url = explode('&page=',$_SERVER['REQUEST_URI']);
		$url = $url[0];
		$page_nav[] = '<span class="page"><a href="'.$url.'&amp;page='.$i.'">'.$i.'</a></span>';
	}

	$i = $page;
	while($i < ($page + 5) && $i <= $nb_page) {
		$url = explode('&page=',$_SERVER['REQUEST_URI']);
		$url = $url[0];
		$page_nav[] = '<span class="page"><a href="'.$url.'&amp;page='.$i.'">'.$i.'</a></span>';
		$i++;
	}

	if($page_nav[1]) {
		sort($page_nav);
		$i = 0;
		while($page_nav[$i]) {
			$page_nav_r .=  $page_nav[$i];
			$i++;
		}
	}

	$result = array($borne_haut, $borne_bas, $page_nav_r);
	return $result;
}

// Couper une chaine trop longue.
function crop($txt, $taille)
{
	if (strlen($txt)<=$taille) return $txt;

	for ($i = 0; $i<$taille; $i++)
		$result .= $txt[$i];
	return $result .= " [...]";
}

// Verif flags binaires.
function verif_flag($flags,$flag) {
	return (bool)($flags & $flag);
}

// Génération du menu.
function load_menu() 
{
	global $config;
	$i = 0;
	
	$menu = array(
		'<a href="'.$config['board_uri'].'" name="'._("accueil").'">'._("accueil").'</a> - ',
		'<a href="?p=search" name="'._("rechercher").'">'._("rechercher").'</a> - ',
		'<a href="?p=members" name="'._("membres").'">'._("membres").'</a> - ',
	);
	
	while($menu[$i]) {
		$result .= $menu[$i];
		$i++;
	}
	
	if(check_auth()) {
		$result .= '<a href="?p=controls" name="'._("controles").'">'._("controles").'</a> - ';
		$result .= '<a href="?p=log&amp;action=out" name="'._("logout").'">'._("logout").'</a>';
	} else {
		$result .= '<a href="?p=subscribe" name="'._("inscription").'">'._("inscription").'</a>';
	}
	
	return $result;
}

// Génération du panneau de contrôle.
function load_panel()
{
	global $config;
	if(check_auth()) {
		if(empty($_SESSION['avatar'])) {
			$avatar = $config['dir_avatars'].'/noavatar.png';
		} else {
				$avatar = $config['dir_avatars'].'/'.$_SESSION['idu'].$_SESSION['avatar'];
		}
		$result = '<table class="tab">
				<tr style="vertical-align:top">
					<td>
						 <h3>'._('panel').'</h3>
						 '._('panel_auth').'<ins>'.$_SESSION['username'].'</ins>.<br />'.
						 _('new_msg').'<ins>0</ins>.<br />'.
						 _('new_posts').'<ins>0</ins>.<br />'.
						 _('last_visit').'<ins>soon</ins>.
					</td>
					<td width="'.($config['avatar_xy']).'" style="text-align:right">
						<img src="'.$avatar.'" alt="'.$avatar.'" />
					</td>
				</tr>
			   </table>';
	} else {
		$result = '<h3>'._('panel').'</h3>'.'<table class="tab"><tr class="tr_top"><td>';
		$result .= _('panel_guest');
		$result .= '</td><td id="login"><div>
				<form method="post" action="?p=log&amp;action=in">
				<fieldset>
					<legend>'._('ident').'</legend>

					<input type="hidden" name="old" value="'.$_SERVER['HTTP_REFERER'] .'" />
					<input type="text" name="username" value="'._('user').'" size="16" maxlength="16" tabindex="1" onfocus="if (this.value ==  \''._('user').'\') this.value = \'\';" />
					<input type="password" name="password" value="'._('pass').'" size="16" maxlength="16" tabindex="2" onfocus="if (this.value == \''._('pass').'\') this.value = \'\';" />
					<input type="submit" name="login" value="'._('okform').'" tabindex="3" />
				</fieldset>
				</form>
			    </div>';
		$result .= '</td></tr></table>';
	}
	return $result;
}

// Gestion des modules.
function load_main() 
{
	if(empty($_GET['p'])) {
		$result = mod_forums();		
	} else {
		if(function_exists('mod_'.$_GET['p'])) {
			$fonction_var = 'mod_'.$_GET['p'];
			$result = $fonction_var();
		}
	}
	return $result;
}

// Users online
function load_online()
{
	global $config;
	$mysql_1 = mysql_query('SELECT '.$config['mysql_prefix'].'users.id,username FROM
					        '.$config['mysql_prefix'].'users_online,'.$config['mysql_prefix'].'users 
					        WHERE '.$config['mysql_prefix'].'users_online.id='.$config['mysql_prefix'].'users.id');

	$mysql_2 = mysql_query('SELECT ip FROM '.$config['mysql_prefix'].'guests_online');

	$result .= _('invites_online').mysql_num_rows($mysql_2)._('membres_online_1').mysql_num_rows($mysql_1)._('membres_online_2');
	$result .= '<br /><br /><h3>'._('membres_online_3').'</h3>';
	while($user = mysql_fetch_array($mysql_1)) {
		$result .= '<a href="?p=view&id='.$user['id'].'" >'.$user['username'].'</a>, ';
	}
	return $result;
}

function load_stats() 
{
	global $config;
	$users = mysql_query('SELECT COUNT(id) FROM '.$config['mysql_prefix'].'users');
	$users = mysql_result($users,0);
	$nbt = mysql_query('SELECT COUNT(id) FROM '.$config['mysql_prefix'].'topics');
	$nbt = mysql_result($nbt,0);
	$nbm = mysql_query('SELECT COUNT(id) FROM '.$config['mysql_prefix'].'messages');
	$nbm = mysql_result($nbm,0);
	$result .= _('stats1').' <strong>'.$users.'</strong> '._('stats2').' <strong>'.$nbt.'</strong> '._('stats3').' <strong>'.$nbm.'</strong> '._('stats4');
	return $result;
}

// Génération du forum.
function load_theme() 
{
	global $config;
	$t_start = microtime();
	$template = array(
		'%BB_TITLE%',
		'%BB_DESC%',
		'%BB_CSS%',
		'%BB_NAVLINKS%',
		'%BB_PANEL%',
		'%BB_MAIN%',
		'%BB_COPY%'
	);
	
	$html = array(
		$config['board_title'],
		$config['board_desc'],
		'themes/'.$config['board_theme'].'/style.css',
		load_menu(),
		load_panel(),
		load_main(),
		_('copy_start').$config['board_version']._('copy_end')
	);

	$file = fopen('themes/'.$config['board_theme'].'/index.bb', 'r');

	if (strstr($_SERVER['HTTP_ACCEPT '], 'application/xhtml+xml')) {
		header('Content-type: application/xhtml+xml; charset = UTF-8 ');
	} else {
		header('Content-type: text/html; charset = UTF-8 ');
	} 

	while (!feof($file)) {
		$buffer = fgets($file, 4096);
		if(strpos($buffer, '%BB_')) {
			echo str_replace($template, $html, $buffer);
			if(strpos($buffer,'COPY')) {
				echo round(microtime()-$t_start,4);
			}
		} else {
			echo $buffer;
		}
	}
	
	fclose($file);
}

function test() 
{
	$var = 1;

}

?>
